package com.client;

import com.decoder.PacketDecoder;
import com.handler.RxtxHandler;
import com.util.ByteUtil;
import com.util.CRC16Util;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFactory;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.rxtx.RxtxChannel;
import io.netty.channel.rxtx.RxtxChannelConfig;
import io.netty.channel.rxtx.RxtxDeviceAddress;

import java.util.List;
import java.util.Scanner;

public class Slave {
    private String portName;
    private int baudrate;
    RxtxChannel channel;
    public List<String> data;

    public Slave(String portName, int baudrate, List<String> data) {
        this.data = data;
        this.portName = portName;
        this.baudrate = baudrate;
    }

    public void run() {
        try {
            OioEventLoopGroup group = new OioEventLoopGroup();
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channelFactory(new ChannelFactory<RxtxChannel>() {
                        public RxtxChannel newChannel() {
                            return channel;
                        }
                    })
                    .handler(new ChannelInitializer<RxtxChannel>() {
                        @Override
                        public void initChannel(RxtxChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    new PacketDecoder(),
                                    new RxtxHandler(data)
                            );
                        }
                    });

            channel = new RxtxChannel();
            channel.config().setBaudrate(baudrate);
            channel.config().setDatabits(RxtxChannelConfig.Databits.DATABITS_8);
            channel.config().setParitybit(RxtxChannelConfig.Paritybit.EVEN);
            channel.config().setStopbits(RxtxChannelConfig.Stopbits.STOPBITS_1);
            ChannelFuture future = b.connect(new RxtxDeviceAddress(portName)).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void writeAndFlush(String hexString) {
        String s = CRC16Util.crcDeal(hexString);
        // System.out.println("手动发送数据："+s);
        byte[] bytes = ByteUtil.hexStringToBytes(s);
        ByteBuf buffer = channel.alloc().buffer();
        ByteBuf byteBuf = buffer.writeBytes(bytes);
        channel.writeAndFlush(byteBuf);
    }

    public void inputCmd() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            String s = sc.nextLine();
            writeAndFlush(s);
        }
    }

    public void request() throws InterruptedException {
        String cmd1 = "030300000002";
        String cmd2 = "020300020001";
        String cmd3 = "040300030001";
        Thread.sleep(1000);
        writeAndFlush(cmd1);
        Thread.sleep(1000);
        writeAndFlush(cmd2);
        Thread.sleep(1000);
        writeAndFlush(cmd3);
    }


}
